package com.lxk.builder;

import com.lxk.bean.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

/**
 * 生成基础类
 *
 * @author lxk
 */
public class BuildBase {
    private static Logger logger = LoggerFactory.getLogger(BuildBase.class);

    public static void execute()
    {
        List<String> headerInfoList = new ArrayList<>();
        // 生成枚举包
        headerInfoList.add("package " + Constants.PACKAGE_ENUMS);
        build(headerInfoList, "DateTimePatternEnum", Constants.PATH_ENUMS);
        headerInfoList.clear();

        // 生成工具类包
        headerInfoList.add("package " + Constants.PACKAGE_UTILS);
        build(headerInfoList, "DateUtils", Constants.PATH_UTILS);
        headerInfoList.clear();

        //生成BaseMapper
        headerInfoList.add("package " + Constants.PACKAGE_MAPPERS);
        build(headerInfoList, "BaseMapper", Constants.PATH_MAPPERS);
        headerInfoList.clear();

        //生成PageSize枚举
        headerInfoList.add("package " + Constants.PACKAGE_ENUMS);
        build(headerInfoList, "PageSize", Constants.PATH_ENUMS);
        headerInfoList.clear();

        //生成SimplePage类
        headerInfoList.add("package " + Constants.PACKAGE_Query);
        headerInfoList.add("import " + Constants.PACKAGE_ENUMS + ".PageSize");
        build(headerInfoList, "SimplePage", Constants.PATH_Query);
        headerInfoList.clear();

        //生成BaseQuery(BaseParam)类
        headerInfoList.add("package " + Constants.PACKAGE_Query);
        build(headerInfoList, "BaseQuery", Constants.PATH_Query);
        headerInfoList.clear();

        //生成PaginationResultVO类
        headerInfoList.add("package " + Constants.PACKAGE_VO);
        build(headerInfoList, "PaginationResultVO", Constants.PATH_VO);
        headerInfoList.clear();

        //生成BusinessException类
        headerInfoList.add("package " + Constants.PACKAGE_EXCEPTION);
        headerInfoList.add("import " + Constants.PACKAGE_ENUMS + ".ResponseCodeEnum");
        build(headerInfoList, "BusinessException", Constants.PATH_EXCEPTION);
        headerInfoList.clear();

        //生成ResponseCodeEnum枚举
        headerInfoList.add("package " + Constants.PACKAGE_ENUMS);
        build(headerInfoList, "ResponseCodeEnum", Constants.PATH_ENUMS);
        headerInfoList.clear();

        //生成ResponseVO类
        headerInfoList.add("package " + Constants.PACKAGE_VO);
        build(headerInfoList, "ResponseVO", Constants.PATH_VO);
        headerInfoList.clear();

        //生成ABaseController类
        headerInfoList.add("package " + Constants.PACKAGE_CONTROLLER);
        headerInfoList.add("import " + Constants.PACKAGE_ENUMS + ".ResponseCodeEnum");
        headerInfoList.add("import " + Constants.PACKAGE_VO + ".ResponseVO");
        build(headerInfoList, "ABaseController", Constants.PATH_CONTROLLER);
        headerInfoList.clear();

        //生成AGlobalExceptionHandlerController类
        headerInfoList.add("package " + Constants.PACKAGE_CONTROLLER);
        headerInfoList.add("import " + Constants.PACKAGE_ENUMS + ".ResponseCodeEnum");
        headerInfoList.add("import " + Constants.PACKAGE_VO + ".ResponseVO");
        headerInfoList.add("import " + Constants.PACKAGE_EXCEPTION + ".BusinessException");
        build(headerInfoList, "AGlobalExceptionHandlerController", Constants.PATH_CONTROLLER);

    }

    private static void build(List<String> headerInfoList, String fileName, String outPutPath)
    {
        File folder = new File(outPutPath);
        if (!folder.exists())
        {
            folder.mkdirs();
        }

        File javaFile = new File(outPutPath, fileName+".java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        InputStream in = null;
        InputStreamReader inr = null;
        BufferedReader br = null;
        try{
            out = Files.newOutputStream(javaFile.toPath());
            outw = new OutputStreamWriter(out,"utf-8");
            bw = new BufferedWriter(outw);


            // 直接使用 getResourceAsStream
            in = BuildBase.class.getClassLoader().getResourceAsStream("template/" + fileName + ".txt");
//            String templatePath = BuildBase.class.getClassLoader().getResource("template/"+fileName+".txt").getPath();
//            in = Files.newInputStream(Paths.get(templatePath));
            inr = new InputStreamReader(in,"utf-8");
            br = new BufferedReader(inr);

            for (String headerInfo : headerInfoList){
                bw.write(headerInfo + ";");
                bw.newLine();
                bw.newLine();
            }

            String lineInfo = null;
            while ((lineInfo = br.readLine()) != null){
                bw.write(lineInfo);
                bw.newLine();
            }
            bw.flush();

        }catch (Exception e){
            logger.error("生成基础类{}失败", fileName,e);
        }finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inr != null){
                try {
                    inr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null){
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
